package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.StatusConstant;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zzyl.vo.NursingPageVo;
import com.zzyl.vo.NursingPlanVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    /**
     * 新增护理计划
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addNursingPlan(NursingPlanDto nursingPlanDto) {
        // 创建护理计划对象来接收
        NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class);
        nursingPlan.setStatus(StatusConstant.DISABLE);
        // 向数据库中插入护理计划
        nursingPlanMapper.insertNuringPlan(nursingPlan);
        // 提出护理项目项目计划
        List<NursingProjectPlanDto> nursingProjectPlanDtoList = nursingPlanDto.getProjectPlans();
        // 将护理计划的ID取出来返回给护理项目计划
        List<NursingProjectPlan> aimProjectPlanList = nursingProjectPlanDtoList.stream().map(nursingProjectPlanDto -> {
            NursingProjectPlan nursingProjectPlan = BeanUtil.copyProperties(nursingProjectPlanDto, NursingProjectPlan.class);
            nursingProjectPlan.setPlanId(nursingPlan.getId());
            return nursingProjectPlan;
        }).collect(Collectors.toList());

        // 向数据库中插入护理项目
        nursingPlanMapper.insertNuringProject(aimProjectPlanList);

        // 备注：同一个方法设计到对多张表进行增删改，所以不要忘记加事务。
    }

    /**
     * 条件分页查询
     */
    @Override
    public PageResponse<NursingPlanVo> getPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        // 设置分页条件
        PageHelper.startPage(pageNum, pageSize);
        // 进行普通查询，并得到分页结果
        Page<NursingPlanVo> nursingPlanVoPage = nursingPlanMapper.getPage(name, status);
        return PageResponse.of(nursingPlanVoPage, NursingPlanVo.class);
    }

    /**
     * 查询所有的护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlanVo> getAllNursingPlan() {
        return nursingPlanMapper.getAllNursingPlan();
    }

    /**
     * 根据ID回显信息
     *
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getInfoByID(Integer id) {
        //1.通过 id 连表查询
        return nursingPlanMapper.getInfoByID(id);
    }

    /**
     * 编辑护理计划信息
     * @param nursingPlanDto
     * @return
     */
    @Override
    public void updatePlanInfo(NursingPlanDto nursingPlanDto) {
        //1.转换数据类型
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        //2.补全信息
        //2.1补全np
        nursingPlan.setCreateBy(1668522280851951617L);
        //2.2补全npp
        List<NursingProjectPlan> projectPlanList = nursingPlanDto.getProjectPlans().stream().map(npp -> {
            NursingProjectPlan nursingProjectPlan = BeanUtil.toBean(npp, NursingProjectPlan.class);
            nursingProjectPlan.setPlanId(nursingPlan.id);
            return nursingProjectPlan;
        }).collect(Collectors.toList());
        //3.逻辑处理
        //删除原有的中间表
        nursingPlanMapper.deleteProjectPlan(nursingPlan.getId());
        //4.mapper处理
        //4.1更新计划信息
        nursingPlanMapper.updatePlanInfo(nursingPlan);
        //4.2添加中间表信息
        nursingPlanMapper.addNPPInfo(projectPlanList);
    }

    /**
     * 根据id删除护理计划
     *
     * @param id
     * @return
     */
    @Override
    public void deletePlanById(Integer id) {
        //1.删除逻辑
        //1.1计划:直接删除
        //1.2中间表:直接删除
        //2.执行删除
        nursingPlanMapper.deletePlanById(id);
        nursingPlanMapper.deleteProjectPlan(Long.parseLong(id + ""));
    }
    /**
     * 根据ID更改计划状态
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public void updateStatus(Integer id, Integer status) {
        nursingPlanMapper.updateStatus( id,  status);
    }
}
